package com.peycard.business.facade;

import java.util.Map;

import javax.ejb.EJB;
import javax.ejb.Stateless;

import com.peycard.business.BusinessException;
import com.peycard.business.facade.local.TagParserCryptoBeanFacadeLocal;
import com.peycard.business.local.TDESBeanLocal;
import com.peycard.business.local.TagParserBeanLocal;
import com.peycard.common.Tag;
import com.peycard.common.util.ReaderTagList;
import com.peycard.common.util.TagList;

/**
 * Session Bean implementation class TagParserCryptoBeanFacade
 */
@Stateless
public class TagParserCryptoBeanFacade implements TagParserCryptoBeanFacadeLocal {

	@EJB
	private TagParserBeanLocal tagParserBean;
	@EJB
	private TDESBeanLocal tripleDeasBean;
	
	private static String BDK = "0123456789ABCDEFFEDCBA9876543210";
	
    /**
     * Default constructor. 
     */
    public TagParserCryptoBeanFacade() {
        // TODO Auto-generated constructor stub
    }
    
    /* (sin Javadoc)
     * @see com.peycard.business.facade.local.TagParserCryptoBeanFacadeLocal#getEMVTags(java.lang.String)
     */
    @Override
    public Map<TagList, Tag> getEMVTags(String readerOriginalString) throws BusinessException{
    	Map<ReaderTagList, Tag> mapaTagsReader = tagParserBean.parseProprietaryTags(readerOriginalString);
    	Tag tagC0 = mapaTagsReader.get(ReaderTagList.C0);
    	Tag tagC2 = mapaTagsReader.get(ReaderTagList.C2);
    	String clearData = tripleDeasBean.getClearData(tagC2.getValue(), tagC0.getValue(), BDK);
    	Map<TagList, Tag> mapaTagsEMV = tagParserBean.parsePlainTagText(clearData);
    	return mapaTagsEMV;
    }

}
